import { Injectable } from '@angular/core';

declare const CryptoJS: any;

/**
 * A service interacts with backend Web Socket server.
 */
@Injectable()
export class EncryptionService {
    constructor() { }

    encryptAsAES(plaintext: string, secretKey: string): string {
        // let key = CryptoJS.enc.Hex.parse('000102030405060708090a0b0c0d0e0f');
        // let iv = CryptoJS.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
        // let encryptedText = CryptoJS.AES.encrypt("Message", key, { iv: iv });
        // let decryptedText = CryptoJS.AES.decrypt(encryptedText, key, { iv: iv }).toString(CryptoJS.enc.Utf8);

        // console.log(`encryptedText: ${encryptedText}  decryptedText: ${decryptedText}`);

        return CryptoJS.AES.encrypt(plaintext, secretKey);
    }

    decryptAES(encryptedText: string, secretKey: string): string {
        let bytes = CryptoJS.AES.decrypt(encryptedText, secretKey);
        let plaintext = bytes.toString(CryptoJS.enc.Utf8);

        return plaintext;
    }
}